package com.yami.shop.user.multishop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.param.CustomerManagerParam;
import com.yami.shop.bean.param.UserExcelParam;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.UserService;
import com.yami.shop.user.common.service.UserMemberService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

@RestController
@RequestMapping("/user/user")
@Tag(name = "会员管理")
public class UserMemberController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserMemberService userMemberService;


    @GetMapping("/pageCustomer")
    @Operation(summary =  "分页获取客户列表", description = "分页获取客户列表")
    public ServerResponseEntity<IPage<CustomerManagerParam>> pageCustomer(PageParam<CustomerManagerParam> page,@ParameterObject CustomerManagerParam reqParam) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        reqParam.setShopId(shopId);
        IPage<CustomerManagerParam> customerPage = userService.pageCustomerByMultiShop(page, reqParam);
        return ServerResponseEntity.success(customerPage);
    }


    @GetMapping("/exportCustomer")
    @Operation(summary =  "导出客户信息", description = "导出客户信息")
    public void exportCustomer(@ParameterObject CustomerManagerParam customerParam, HttpServletResponse response) {
        Long brandId = SecurityUtils.getShopUser().getShopId();
        customerParam.setShopId(brandId);
        userMemberService.exportCustomer(customerParam, response);
    }

    @GetMapping("/downloadTemplate")
    @Operation(summary =  "下载导入模板（0:客户, 1:会员）", description = "下载导入模板（0:客户, 1:会员）")
    public void downloadTemplate(Integer templateType, HttpServletResponse response) {
        userMemberService.downloadTemplate(templateType, response);
    }

    @PostMapping("/importCustomerExcel")
    @Operation(summary =  "导入客户信息", description = "导入客户信息")
    public ServerResponseEntity<UserExcelParam> importCustomerExcel(@RequestParam("excelFile") MultipartFile excelFile) throws Exception {
        if (Objects.isNull(excelFile)) {
            // 网络繁忙，请稍后重试
            throw new YamiShopBindException("yami.network.busy");
        }
        Long shopId = SecurityUtils.getShopUser().getShopId();
        UserExcelParam userExcelParam = userMemberService.importCustomer(shopId, excelFile);
        return ServerResponseEntity.success(userExcelParam);
    }


}
